﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Catsland.Core
{
    public class AnimationClip
    {
        public String _name { get; set; }
        public int _beginIndex { get; set; }
        public int _endIndex { get; set; }
        public enum PlayMode
        {
            CLAMP,
            LOOP,
            PINGPONG,
            STOP
        };
        public PlayMode _mode { get; set; }

        public AnimationClip(String name, int begin_index = 0, int end_index = 0, PlayMode mode = PlayMode.CLAMP)
        {
			_name = name;
            _beginIndex = begin_index;
            _endIndex = end_index;
            _mode = mode;
        }

		public bool SaveToNode(XmlNode node, XmlDocument doc)
		{
			XmlElement clip = doc.CreateElement("AnimationClip");
			node.AppendChild(clip);

			clip.SetAttribute("name", _name);
			clip.SetAttribute("beginIndex", "" + _beginIndex);
			clip.SetAttribute("endIndex", "" + _endIndex);
			String mode = "";
			switch (_mode)
			{
				case PlayMode.CLAMP:
					mode = "CLAMP";
					break;
				case PlayMode.LOOP:
					mode = "LOOP";
					break;
				case PlayMode.PINGPONG:
					mode = "PINGPONG";
					break;
				case PlayMode.STOP:
					mode = "STOP";
					break;
			}
			clip.SetAttribute("mode", mode);

			return true;
		}

		public static AnimationClip LoadFromNode(XmlNode node, Scene scene)
		{
			XmlElement clip = (XmlElement)node;
			String name = clip.GetAttribute("name");
			int beginIndex = int.Parse(clip.GetAttribute("beginIndex"));
			int endIndex = int.Parse(clip.GetAttribute("endIndex"));
			String modeString = clip.GetAttribute("mode");
			PlayMode mode = PlayMode.STOP;
			switch (modeString)
			{
				case "CLAMP":
					mode = PlayMode.CLAMP;
					break;
				case "LOOP":
					mode = PlayMode.LOOP;
					break;
				case "PINGPONG":
					mode = PlayMode.PINGPONG;
					break;
				case "STOP":
					mode = PlayMode.STOP;
					break;
			}
			AnimationClip newClip = new AnimationClip(name, beginIndex, endIndex, mode);
			return newClip;
		}
	}
}
